var QQMapWX = require('/config/qqmap-wx-jssdk1.2.min.js');
let qqmapsdk = new QQMapWX({
  key: 'ORSBZ-LATWK-W26JT-A35TE-ECYZ7-F4BB6' // 必填
});
App({
  onLaunch: function (options) {
    console.log(options)
    if (options.query.inviteCode){
      wx.setStorageSync('invitationCode', options.query.inviteCode)
    }else if(options.query.scene){
      wx.setStorageSync('invitationCode',decodeURIComponent(options.query.scene))
    }
    this.chkLogin()
    this.upgradeTip()
  },
  //session_key失效清除所有缓存
  chkLogin(){
    if(wx.getStorageSync('token')){
      wx.checkSession({
        success (res) {
        },
        fail (err) {
          wx.clearStorageSync()
        }
      })
    }
  },
  //版本升级提示
  upgradeTip(){
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  //授权位置
  location(successFun, failFun) {
    var that = this;
    return new Promise((resovle, reject) => {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          that.globalData.chkLocation = true
          that.globalData.longitude = res.longitude
          that.globalData.latitude = res.latitude
          resovle(that.getPlace(res.longitude, res.latitude))
        },
        fail: function () {
          wx.showModal({
            title: '温馨提示',
            content: '需要获取您的地理位置，请确认授权，否则无法获取到商家信息',
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  success: (res) => {
                    if (res.authSetting["scope.userLocation"] === true) { //如果用户重新同意了授权登录
                      wx.getLocation({
                        type: 'wgs84',
                        success: function (res) {
                          that.globalData.chkLocation = true
                          that.globalData.longitude = res.longitude
                          that.globalData.latitude = res.latitude
                          resovle(that.getPlace(res.longitude, res.latitude))
                        },
                        fail: function (err){
                          reject('用户拒绝调用经纬度')
                        }
                      })
                    }
                  }
                })
              }else if (res.cancel) {
                reject('用户拒绝调用经纬度')
              }
            }
          })
        }
      })
    })
  },
  //逆地址解析
  getPlace(longitude, latitude) {
    let that = this
    return new Promise((resovle, reject) => {
      qqmapsdk.reverseGeocoder({
        location: {
          longitude,
          latitude
        },
        success: function (res) {
          console.log(res)
          var res = res.result
          that.globalData.detailedDddress = res.address
          that.globalData.address = res.address_component.province + res.address_component.city
          that.globalData.province = res.address_component.province
          that.globalData.city = res.address_component.city
          that.globalData.adcode = res.ad_info.adcode
          resovle(res)
        },
        fail: function (error) {
          reject(error)
        }
      })
    })
  },
  //地址解析（地址转坐标）
  getcoordinate(address){
    var _this = this;
    return new Promise((resovle,reject) => {
      qqmapsdk.geocoder({
        address: address,
        success: function(res) {
          var res = res.result
          resovle(res)
        },
        fail: function(error) {
          reject(error)
        },
      })
    })
  },
  //提示
  $tip(msg, icon = 'none') {
    wx.showToast({
      title: msg,
      icon: icon == 'success' ? 'success' : 'none'
    })
  },
  globalData: {
    userInfo: null,
    chkLocation: false,       //是否有经纬度
    detailedDddress: '',      //详细地址
    longitude: '',            //经度
    latitude: '',             //纬度
    address: '',              //省市
    province: '',             //省
    city: '',                 //市
    adcode: ''
  }
})